/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.shooter;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.ParticleEffect;
import com.badlogic.gdx.graphics.g2d.ParticleEmitter;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.Array;
import com.shooter.SpaceBuddie.Baddies;
import com.shooter.level.ScoreManager;
import java.security.SecureRandom;

/**
 *
 * @author Unni Vemancheri Mana
 */
public class Player{
    
 private int x;
 private int y;
 private int delta = 3;
 private int jetX = 0;
 private int jetY = 0;
 
 private SpriteBatch batch;
 private Texture texture;
 private final String PLAYERSPRITE = "player.png";
 private Texture jetTexture;
 private final String JETSPRITE = "jets.png";
 private Sprite sprite;
 Array<ParticleEmitter> emitters;
 private ParticleEffect effect;
 private int particleCount = 10;
 private int emitterIndex;
 private BitmapFont starFfont;
 private Sky sky;
 private int count = 500;
 private Star[] stars = new Star[count];
 private Star star;
 
 private Vector2 velocity = new Vector2(150, 30);
 private Vector2 position = new Vector2();
 
 private SpaceBuddie spaceBuddie;
  
 private Bomber bomber ;
 
 private boolean isBomber ;
 
 private Explosion explosion;
 
 private boolean noMove = false; // this variable is used to determine whether the player was exploded
 
 private NavigationPanel panel;

 private BulletManager bullet;
 
 private boolean fireBullet= false;
 
 private Input input;
 
 private Texture backGroundTexture;
 
 private SpriteExplode se;
 
 private boolean isExploded;
 
 private ScoreManager scoreManager;
 
 private Rectangle bounds;
 
 private Sound fireSnd;
 
 public Player(){
     
   batch = new SpriteBatch();
   Texture.setEnforcePotImages(false);
   texture = new Texture(Gdx.files.internal(PLAYERSPRITE));
 //jetTexture = new Texture(Gdx.files.internal(JETSPRITE));
   x = 150;
   y = 30;
   sprite = new Sprite(texture);
   sprite.scale(1.5f);
   
   //loading particle effect
   initializeEffect();
    sky = new Sky();
    starFfont = new BitmapFont();
    InitializeStars();
    
    //initialize space buddies
    initializeBaddie();
    
    // initialize bomber 
    bomber = new Bomber(this);
    
    // initialize score manager
    scoreManager = new ScoreManager();
    // initialize explosion
    explosion = new Explosion(scoreManager);
    
    // initialize navigation panel
    panel = new NavigationPanel();
    panel.initialize();
    
    // initialize player bullet
       bullet = new BulletManager();
    // end of initialization
       
     // loading back ground image
        backGroundTexture =  new Texture(Gdx.files.internal("Background4.png"));   
        
    // initialize player explosion
       se    = new SpriteExplode("ship3.png"); 
       bounds = new Rectangle();
       bounds.height = sprite.getHeight();
       bounds.width = sprite.getWidth();
 }
 
 private void initializeEffect(){
   effect = new ParticleEffect();
   effect.load(Gdx.files.internal("particle1.p"), Gdx.files.internal(""));
   effect.start();
   emitters = new Array(effect.getEmitters());
   effect.getEmitters().clear();
   effect.getEmitters().add(emitters.get(0));
 }
 
 public int getX(){
  return x;    
 }
 
 public int getY(){
  return y;    
 }
 
 public void setFireBullet(boolean fireBullet){
     this.fireBullet = fireBullet;
     if(fireBullet)
     bullet.getBullet(x, y);
 }
 
 public void setBulletManager(BulletManager bullet){
     this.bullet = bullet;
 }
 
 public void setFiringSound(Sound fireSnd){
     this.fireSnd = fireSnd;
 }
 public Sound getFiringSound(){
     return fireSnd;
 }
 
 public BulletManager getBulletManager(){
     return bullet;
 }
 public boolean getFireBullet(){
     return fireBullet;
 }
 
 public Sprite getSprite(){
     return sprite;
 }
 
 public void drawPlayer(boolean isBomber){
//batch.getProjectionMatrix().setToOrtho2D(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
  this.isBomber = isBomber;
  Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);   
  float d = Gdx.graphics.getDeltaTime();
  batch.begin();
   drawBackGround();
  //sprite.setPosition(x, y);
  sprite.setPosition(position.x, position.y);
  sprite.draw(batch);
  drawRandomDots();
  drawJet(d);
  drawSpaceBuddie();
  //draw Bomber
  drawBomber();
  // draw explosion
  //explosion.explode(batch,x,y);
  // draw navigationpanel
  panel.drawNavigationPanel(batch);
  // check bulet is fired
  fireBullet();
  //draw player explosion
  drawPlayerExplosion();
 
  batch.end();
 }
 
 private void fireBullet(){
     // check collision witn enemies
   Baddies [] buddies = spaceBuddie.getBuddies();
     // end of collision
     if(fireBullet){
        bullet.drawBullet(batch,this, spaceBuddie);
     }
 }
 
 public void moveLeft(){
  if(position.x < 15) return; 
  position.x-= delta + Gdx.graphics.getDeltaTime();
  bounds.x = position.x;
 }

 public void moveRight(){
  if(position.x > 460) return;     
  position.x+= delta + Gdx.graphics.getDeltaTime();
  bounds.x = position.x;
 }
 
 public void move(Vector3 pos){
   x = (int)pos.x;
   y = (int)pos.y;
   System.out.println(" ---Vector3 player x " + x);
   System.out.println(" ---Vector3 player y " + y);
   if(x > 300) x = 300;
 }
 
 public void move(int x0, int y0){
   x = x0;
   y = y0;
   System.out.println(" --- player x " + x);
   System.out.println(" --- player y " + y);
   if(x > 300) x = 300;
 }
 
 public void moveUp(){
  if( y >= 310) {
    y = 310;  
    return;
  }    
  y = y + delta -4;   
  System.out.println(" moveUp it getting callled");
 }
 
 public void moveDown(){
   y =  y - delta +4;      
 }
 
 private void drawJet(float d){
   jetY = y - 12;
   jetX = x;
   effect.setPosition(x + 8, y - 5);
   effect.draw(batch, d);
 }
 
 private void drawRandomDots(){
   for(int i =0; i<count; i++){
      Star s = stars[i];
      if(s.y < 0)s.y = 420;
      starFfont.draw(batch, ".", s.x,s.y);
      s.y = s.y - 1.0f;
   }
 }

 private void InitializeStars(){
   star = new Star();
   star.x = 10;
   //star.y = 330;
   star.y = 420;
   for(int i =0; i<count;i++){
          Star s = new Star();
          s.x = new SecureRandom().nextInt(470);
          s.y = new SecureRandom().nextInt(420);
          stars[i] = s;
   }
 }
 
 private void initializeBaddie(){
   spaceBuddie = new SpaceBuddie(); 
 }
 
private void drawSpaceBuddie(){
  spaceBuddie.drawSpaceBuddie(batch);
} 

private void drawBomber(){
   if(isBomber){
     bomber.drawBomber(batch); 
   }
}

 public void loadBomber(){
    bomber.loadBomber(x, y);
 }

 
 public void drawExplosion(){
     
 }

 
 public void setDelta(int delta){
     this.delta = delta;
 }
 
 public void setNoMOve(boolean noMove){
     this.noMove = noMove;
 }
 
 public Explosion getExplosion(){
    return explosion; 
 }
 
 public Texture getTexture(){
     return texture;
 }
 
 private void drawBackGround(){
     batch.draw(backGroundTexture, -50, -50);          
 }
   
  private void drawPlayerExplosion(){
  //if(scoreManager.getPlayerHit() > 3){
     //initializePlayerExplosion();
     //scoreManager.resetPlayerHit();
   //}
   if(isExploded){   
     se.drawSpriteExplosion(batch);              
   }
  } 
  
  public void initializePlayerExplosion(){
   isExploded = true;
   se.setAnimated(true);
   se.initialize();
   se.updatePosition(x, y);
  }
  
  public void update(float delta){
      position.add(velocity.tmp().mul(delta));
  }
  
  public void setPosition(Vector2 vec){
      this.position = vec;
      bounds.x= vec.x;
  }
  
  public Vector2 getPosition(){
      return position;    
  }
  
  public Rectangle getBounds(){
   return bounds;    
  }
  
  private class Star {
        private float x;
        private float y;
        private int speed = 1;
  }
}
